package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanProjectService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: NursingPlanServiceImpl
 * @description:
 * @author: InnovatorX
 * @date: 2024/5/27 10:23
 * @Version: 1.0
 */
@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingPlanProjectService nursingPlanProjectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setStatus(1);

        //保存计划表
        nursingPlanMapper.savePlan(nursingPlan);
        // 获取主键ID
        Long planId = nursingPlan.getId();
        log.info("计划表保存成功，计划ID：{}", planId);
        //批量保存中间表
        nursingPlanProjectService.saveBatch(dto.getProjectPlans(), planId);
    }

    @Override
    public List<NursingPlanVo> findAllPlan() {
        List<NursingPlan> list = nursingPlanMapper.findAllPlan();
        return BeanUtil.copyToList(list, NursingPlanVo.class);
    }

    @Override
    public PageResponse<NursingPlanVo> findByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<NursingPlanVo> nursingPlanVos = nursingPlanMapper.selectByPage(name, status);

        Page<NursingPlanVo> planVoPage = (Page<NursingPlanVo>) nursingPlanVos;

        return PageResponse.of(planVoPage, NursingPlanVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        nursingPlanMapper.deleteById(id);
        nursingPlanProjectService.deleteById(id);
    }

    @Override
    public NursingPlanVo findById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.findByIdPlan(id);
        List<NursingPlanProjectVo> list = nursingPlanProjectService.findByIdProject(id);

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    @Override
    public void update(Long id, NursingPlanDto dto) {
        //封装计划表
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        // 修改计划表
        nursingPlanMapper.updateById(nursingPlan);
        //获取计划表ID
        Long planId = dto.getId();
        nursingPlanProjectService.deleteById(planId);

        nursingPlanProjectService.saveBatch(dto.getProjectPlans(), planId);
    }

    @Override
    public void updateBySta(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateById(nursingPlan);
    }
}
